package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.utils.BeanCopyUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.bo.TBsCalendarBo;
import com.ruoyi.system.domain.vo.TBsCalendarVo;
import com.ruoyi.system.api.domain.TBsCalendar;
import com.ruoyi.system.mapper.TBsCalendarMapper;
import com.ruoyi.system.service.ITBsCalendarService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 工作日Service业务层处理
 *
 * @author chq
 * @date 2025-06-16
 */
@RequiredArgsConstructor
@Service
public class TBsCalendarServiceImpl implements ITBsCalendarService {

    private final TBsCalendarMapper baseMapper;

    /**
     * 查询工作日
     */
    @Override
    public TBsCalendarVo queryById(String natureday){
        return baseMapper.selectVoById(natureday);
    }

    /**
     * 查询工作日列表
     */
    @Override
    public TableDataInfo<TBsCalendarVo> queryPageList(TBsCalendarBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TBsCalendar> lqw = buildQueryWrapper(bo);
        Page<TBsCalendarVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询工作日列表
     */
    @Override
    public List<TBsCalendarVo> queryList(TBsCalendarBo bo) {
        LambdaQueryWrapper<TBsCalendar> lqw = buildQueryWrapper(bo);
        List<TBsCalendarVo> tBsCalendarVos = baseMapper.selectVoList(lqw);
        return tBsCalendarVos;
    }

    @Override
    public TBsCalendar query(TBsCalendarBo bo) {
        return BeanCopyUtils.copy(queryList(bo).get(0), TBsCalendar.class);
    }

    private LambdaQueryWrapper<TBsCalendar> buildQueryWrapper(TBsCalendarBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TBsCalendar> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getNatureday()), TBsCalendar::getNatureday, bo.getNatureday());
        lqw.eq(StringUtils.isNotBlank(bo.getDealflag()), TBsCalendar::getDealflag, bo.getDealflag());
        lqw.eq(StringUtils.isNotBlank(bo.getHkdealflag()), TBsCalendar::getHkdealflag, bo.getHkdealflag());
        lqw.eq(StringUtils.isNotBlank(bo.getIsMonthLastworkday()), TBsCalendar::getIsMonthLastworkday, bo.getIsMonthLastworkday());
        lqw.eq(StringUtils.isNotBlank(bo.getIsMonthFirstworkday()), TBsCalendar::getIsMonthFirstworkday, bo.getIsMonthFirstworkday());
        lqw.eq(StringUtils.isNotBlank(bo.getIsSeasonLastworkday()), TBsCalendar::getIsSeasonLastworkday, bo.getIsSeasonLastworkday());
        lqw.eq(StringUtils.isNotBlank(bo.getIsSeasonFirstworkday()), TBsCalendar::getIsSeasonFirstworkday, bo.getIsSeasonFirstworkday());
        return lqw;
    }

    /**
     * 新增工作日
     */
    @Override
    public Boolean insertByBo(TBsCalendarBo bo) {
        TBsCalendar add = BeanUtil.toBean(bo, TBsCalendar.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setNatureday(add.getNatureday());
        }
        return flag;
    }

    /**
     * 修改工作日
     */
    @Override
    public Boolean updateByBo(TBsCalendarBo bo) {
        TBsCalendar update = BeanUtil.toBean(bo, TBsCalendar.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TBsCalendar entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除工作日
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public TBsCalendar getLastWorkDay(String dateEight) {
        LambdaQueryWrapper<TBsCalendar> lqw = new QueryWrapper<TBsCalendar>().lambda();
        lqw.lt(TBsCalendar::getNatureday, dateEight)
            .eq(TBsCalendar::getDealflag, "0")
            .orderByDesc(TBsCalendar::getNatureday)
            .last("limit 1");
        return baseMapper.selectOne(lqw);
    }
}
